package org.mybatis.generator.codegen.mybatis3.javaservice;

import org.codehaus.plexus.util.StringUtils;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.codegen.AbstractJavaGenerator;

import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

public class WebControllerGenerator extends AbstractJavaGenerator{
	Properties properties;
	public WebControllerGenerator(Properties properties){
		this.properties=properties;
	}
	String domainName;
	 @Override
	    @SuppressWarnings({ "rawtypes", "unchecked" })
	    public List<CompilationUnit> getCompilationUnits() {
	        String pack = properties.getProperty("targetControllerPackage");
	        String service = properties.getProperty("targetServicePackage");
	        
	        String vo = properties.getProperty("targetVoPackage");
	        domainName=introspectedTable.getFullyQualifiedTable().getDomainObjectName();
	        String controllerName=
		            new StringBuffer(pack).append(".").
			          append(domainName).  
			          append("Controller").toString();
	        FullyQualifiedJavaType type = new FullyQualifiedJavaType(controllerName);
	        
	        TopLevelClass inter = new TopLevelClass(type);
	        inter.setVisibility(JavaVisibility.PUBLIC);  
	        inter.addImportedType(new FullyQualifiedJavaType  
	                     (introspectedTable.getBaseRecordType())); 
	        
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		(new StringBuffer(service).append(".").
	      		          append(domainName).  
	    		          append("Service").toString()));
	        
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		(new StringBuffer(vo).append(".").
	        				append(domainName).  
	        				append("Vo").toString()));
	        
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.beans.factory.annotation.Autowired"));
//	        inter.addImportedType(new FullyQualifiedJavaType
//	        		("org.slf4j.Logger"));
//	        inter.addImportedType(new FullyQualifiedJavaType
//	        		("org.slf4j.LoggerFactory"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.http.HttpStatus"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.http.ResponseEntity"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.stereotype.Controller"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.web.bind.annotation.ModelAttribute"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.web.bind.annotation.PathVariable"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.web.bind.annotation.RequestMapping"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.web.bind.annotation.RequestMethod"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.web.bind.annotation.ResponseBody"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("com.github.pagehelper.PageInfo"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("com.gorilla.core.BaseRsp"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.ui.ModelMap"));
	        inter.addImportedType(new FullyQualifiedJavaType  
	        		("org.springframework.beans.BeanUtils"));
		 inter.addImportedType(new FullyQualifiedJavaType("lombok.extern.slf4j.Slf4j"));
	        inter.addAnnotation("@Slf4j");
	        inter.addAnnotation("@Controller");
	        inter.addAnnotation("@RequestMapping(\""+domainName.toLowerCase()+"\")");
	      
//	        Field field = new Field();
//	        field.setVisibility(JavaVisibility.PRIVATE);
//	        field.setStatic(true);
//	        field.setFinal(true);
//	        field.setType(new FullyQualifiedJavaType("Logger"));
//
//	        field.setName("log=LoggerFactory.getLogger("+domainName+"Controller.class);");
//	        inter.addField(field);
	        
	        Field field2 = new Field();
	        field2.addAnnotation("@Autowired");
	        field2.setVisibility(JavaVisibility.PRIVATE);
	        field2.setType(new FullyQualifiedJavaType  
	        		(new StringBuffer().
	        				append(domainName).  
	        				append("Service").toString()));
	        field2.setName(StringUtils.uncapitalise(domainName+"Service"));
	        inter.addField(field2);
	        
	        inter.addMethod(findByPage());
	        inter.addMethod(deleteById());
	        inter.addMethod(findById());
	        inter.addMethod(save());
	        
	        List answer = new ArrayList();
	        answer.add(inter);  
	        return answer;  
	    }  
	 
	 
	 	public  Method findByPage(){
	 		Method method=new Method();
	 		method.addJavaDocLine("/**");
	        StringBuilder sb = new StringBuilder();
	        sb.append(" * ");
	        sb.append("分页查询获取对象");
	        method.addJavaDocLine(sb.toString());
	        method.addJavaDocLine(" */");
	        method.addAnnotation("@RequestMapping(value =\"/findByPage/{pageNum}/{pageSize}\" ,method = RequestMethod.GET)");
	        method.setVisibility(JavaVisibility.PUBLIC);
	        method.setName("findByPage");
	        method.addParameter(new Parameter(
	        		new FullyQualifiedJavaType("@PathVariable(\"pageNum\") int"), "pageNum"));
	        method.addParameter(new Parameter(
	        		new FullyQualifiedJavaType("@PathVariable(\"pageSize\") int"), "pageSize") );
	        method.addParameter(new Parameter(
	        		new FullyQualifiedJavaType("ModelMap"), "model") );
	        
	        StringBuilder sb1 = new StringBuilder();
	        sb1.append(" model.clear();\n");
	        sb1.append(" PageInfo<"+domainName+">  pages=this."+StringUtils.uncapitalise(domainName+"Service")+".queryListPage(null, pageNum, pageSize);\n");
	        sb1.append(" model.addAttribute(\"vo\", pages);\n");
	        sb1.append("   return \"\";");
	        method.addBodyLine(sb1.toString());
	        FullyQualifiedJavaType fqjt =new FullyQualifiedJavaType("String");
	        method.setReturnType(fqjt);
	        return method;
	 	}
	 	
	 	public  Method deleteById(){
	 		Method method=new Method();
	 		method.addJavaDocLine("/**");
	        StringBuilder sb = new StringBuilder();
	        sb.append(" * ");
	        sb.append("删除对象");
	        method.addJavaDocLine(sb.toString());
	        method.addJavaDocLine(" */");
	        
	        method.addAnnotation("@RequestMapping(value =\"/delete/{id}\" ,method = RequestMethod.DELETE)");
	        method.addAnnotation("@ResponseBody");
	        method.setVisibility(JavaVisibility.PUBLIC);
	        method.setName("deleteById");
	        method.addParameter(new Parameter(
	        		new FullyQualifiedJavaType("@PathVariable(\"id\") int"), "id") );
	        
	        StringBuilder sb1 = new StringBuilder();
	        sb1.append("                 ").append("int count=this."+StringUtils.uncapitalise(domainName+"Service")+".deleteById(id);").append("\n");
	        sb1.append("                 ").append("BaseRsp base=new BaseRsp();").append("\n");
	        sb1.append("             ").append("if(count>0){").append("\n");
	        sb1.append("                 ").append("base.setCode(HttpStatus.OK.value());").append("\n");
	        sb1.append("                 ").append("base.setMsg(\"success\");").append("\n");
	        sb1.append("                 ").append("return 	new ResponseEntity<BaseRsp>(base, HttpStatus.FOUND);").append("\n");
	        sb1.append("             ").append("}else{").append("\n");
	        sb1.append("                 ").append("base.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());").append("\n");
	        sb1.append("                 ").append("base.setMsg(\"error\");").append("\n");
	        sb1.append("                 ").append("return new ResponseEntity<BaseRsp>(base, HttpStatus.INTERNAL_SERVER_ERROR);").append("\n");
	        sb1.append("             ").append("}");
	        method.addBodyLine(sb1.toString());
	        FullyQualifiedJavaType fqjt =new FullyQualifiedJavaType("ResponseEntity<BaseRsp>");
	        method.setReturnType(fqjt);
	        return method;
	 	}
	 	
	 	public  Method findById(){
	 		Method method=new Method();
	 		method.addJavaDocLine("/**");
	        StringBuilder sb = new StringBuilder();
	        sb.append(" * ");
	        sb.append("根据ID获取对象");
	        method.addJavaDocLine(sb.toString());
	        method.addJavaDocLine(" */");
	        method.addAnnotation("@RequestMapping(value =\"/findById/{id}\" ,method = RequestMethod.GET)");
	        method.addAnnotation("@ResponseBody");
	        method.setVisibility(JavaVisibility.PUBLIC);
	        method.setName("findById");
	        method.addParameter(new Parameter(
	        		new FullyQualifiedJavaType("@PathVariable(\"id\") int"), "id") );
	        
	        StringBuilder sb1 = new StringBuilder();
	        sb1.append(domainName+" po=this."+StringUtils.uncapitalise(domainName+"Service")+".queryById(id);").append("\n");
	        sb1.append("     ").append("return new ResponseEntity<"+domainName+">(po, HttpStatus.FOUND);").append("\n");
	        method.addBodyLine(sb1.toString());
	        FullyQualifiedJavaType fqjt =new FullyQualifiedJavaType("ResponseEntity<"+domainName+">");
	        method.setReturnType(fqjt);
	        return method;
	 	}
	 	
	 	
	 	public  Method save(){
	 		Method method=new Method();
	 		method.addJavaDocLine("/**");
	        StringBuilder sb = new StringBuilder();
	        sb.append(" * ");
	        sb.append("保存对象");
	        method.addJavaDocLine(sb.toString());
	        method.addJavaDocLine(" */");
	        method.addAnnotation("@RequestMapping(value =\"/save\" ,method = RequestMethod.POST)");
	        method.addAnnotation("@ResponseBody");
	        method.setVisibility(JavaVisibility.PUBLIC);
	        method.setName("deleteById");
	        method.addParameter(new Parameter(
	        		new FullyQualifiedJavaType("@ModelAttribute(\"vo\") "+domainName+"Vo"),"vo") );
	        
	        StringBuilder sb1 = new StringBuilder();
	        sb1.append("           ").append(domainName+" po=new "+domainName+"();").append("\n");
	        sb1.append("           ").append("BeanUtils.copyProperties(vo, po);").append("\n");
	        sb1.append("           ").append("int count=this."+StringUtils.uncapitalise(domainName+"Service")+".save(po);").append("\n");
	        sb1.append("                 ").append("BaseRsp base=new BaseRsp();").append("\n");
	        sb1.append("              ").append("if(count>0){").append("\n");
	        sb1.append("                 ").append("base.setCode(HttpStatus.OK.value());").append("\n");
	        sb1.append("                 ").append("base.setMsg(\"success\");").append("\n");
	        sb1.append("                 ").append("return 	new ResponseEntity<BaseRsp>(base, HttpStatus.CREATED);").append("\n");
	        sb1.append("              ").append("}else{").append("\n");
	        sb1.append("                 ").append("base.setCode(HttpStatus.INTERNAL_SERVER_ERROR.value());").append("\n");
	        sb1.append("                 ").append("base.setMsg(\"error\");").append("\n");
	        sb1.append("                 ").append("return new ResponseEntity<BaseRsp>(base, HttpStatus.INTERNAL_SERVER_ERROR);").append("\n");
	        sb1.append("             ").append("}");
	        method.addBodyLine(sb1.toString());
	        FullyQualifiedJavaType fqjt =new FullyQualifiedJavaType("ResponseEntity<BaseRsp>");
	        method.setReturnType(fqjt);
	        return method;
	 	}
	 
}
